package ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import control.Constants;

import model.TibNeeg;
import model.YawmSaub;

public class UIFamilyTree extends JPanel{

	/**
	 * Default UID
	 */
	private static final long serialVersionUID = 1L;
	private TibNeeg root;
	private YawmSaub yawmSaub;
	private UIOption option;
	private JPanel pan_family_tree;	
	private JPanel pan_to_color;
	private JPanel pan_selected_to_color;
	private TibNeeg tn_to_color;
	private TibNeeg tn_selected_to_color;
	
	
	/**
	 * Constructor of the family tree panel
	 * @param yawmSaub
	 */
	public UIFamilyTree(YawmSaub yawmSaub, UIOption opt) {
		this.setBorder(BorderFactory.createTitledBorder(" Family Tree "));
		
		this.option = opt;
		setYawmSaub(yawmSaub);
		initRoot();
		this.pan_to_color = null;
		this.pan_selected_to_color = null;
		this.tn_to_color = null;
		this.tn_selected_to_color = null;
		
		add(buildTree());
		
		
	}
	
	/**
	 * Build family tree panel in the Scroll panel
	 * @return the scroll panel contains that the tree
	 */
    public JScrollPane buildTree() {
    	pan_family_tree = new JPanel();
		new UIRecusivePan(pan_family_tree, root, this);
		
		/* Add 2 panel height before and behind The Tree */ 
		JPanel pan = new JPanel();
		pan.setPreferredSize(new Dimension(30, yawmSaub.heightTree(root) * Constants.EACH_CHILD_HEIGTH));
		//pan.setBorder(BorderFactory.createEtchedBorder());
		
		JPanel pan2 = new JPanel();
		pan2.setPreferredSize(new Dimension(30, yawmSaub.heightTree(root) * Constants.EACH_CHILD_HEIGTH));
		//pan2.setBorder(BorderFactory.createEtchedBorder());
		/* */
		
		JPanel p = new JPanel();
		p.add(pan);
		p.add(pan_family_tree);
		p.add(pan2);
		
		JScrollPane scroller_main_pan = new JScrollPane(p);
		scroller_main_pan.setPreferredSize(Constants.DIMENTSION_FAMILY_TREE);
		
		return scroller_main_pan ;
	}
	
    /**
     * Refresh the panel family tree
     */
	public void refresh_tree(){
		this.initRoot();
		this.removeAll();
		this.pan_to_color = null;
		this.add(buildTree());
		this.revalidate();
		this.repaint();
	}
    
	public void initRoot() {
		this.root = this.yawmSaub.getYawgKoobTop();
	}
	

	public void saveImage() throws IOException {
		
		Rectangle r = pan_family_tree.getBounds();
		BufferedImage i = new BufferedImage(r.width, r.height,
		        BufferedImage.TYPE_INT_RGB);
		Graphics g = i.getGraphics();
		paint(g);
		
		String path = null;
		JFileChooser chooser = new JFileChooser();
		int returnVal = chooser.showSaveDialog(pan_family_tree);
	    if (returnVal == JFileChooser.APPROVE_OPTION) {
	    	chooser.getCurrentDirectory();
	    	File file = chooser.getSelectedFile();
	    	path = file.getPath();
	    }
		ImageIO.write(i, "png", new File(path+".png"));
    }
	
	/**
	 * Color the panel label, 3 status possibles ENTERED, EXITED, SELECTED 
	 * @param rootLabel - Current panel label (mouse position)
	 * @param tn - Current TibNeeg (mouse position)
	 * @param color - Color to paint
	 */
	public void choseColorPan(JPanel rootLabel, TibNeeg tn, Color color) {
		
		if(color == Constants.LABEL_SELECTED_BACKGROUND){
			
			colorPan(this.pan_selected_to_color, this.tn_selected_to_color, Constants.LABEL_DEFAULT_BACKGROUND);
			colorPan(rootLabel, tn, color);
			this.pan_selected_to_color = rootLabel;
			this.tn_selected_to_color = tn;
		}else{
		
			if(rootLabel != this.pan_to_color ){
				colorPan(this.pan_to_color, this.tn_to_color, Constants.LABEL_DEFAULT_BACKGROUND);				
				colorPan(rootLabel, tn, Constants.LABEL_ENTER_BACKGROUND);
				this.pan_to_color = rootLabel;
				this.tn_to_color = tn;
				
				colorPan(this.pan_selected_to_color, this.tn_selected_to_color, Constants.LABEL_SELECTED_BACKGROUND);
				// TODO use thread do this job
				
				
			}else{
				colorPan(rootLabel, tn, Constants.LABEL_ENTER_BACKGROUND);
			}
		}
	}
	
	/**
	 * Paint Panel label
	 * @param rootLabel - Current panel label (mouse position)
	 * @param tn - Current TibNeeg (mouse position)
	 * @param color - Color to paint
	 */
	private void colorPan(JPanel rootLabel, TibNeeg tn, Color color) {
		if(rootLabel != null){
			Graphics2D g2d = (Graphics2D) rootLabel.getGraphics();
			Font font = new Font("Verdana", Font.BOLD, 12);
			g2d.setFont(font);
				
			FontRenderContext context = g2d.getFontRenderContext();
			TextLayout txt = new TextLayout(tn.getNpe(), font, context);
			Rectangle2D rect =  txt.getBounds();
				
			// Fill rectangle 
			g2d.setColor(color);
			g2d.fillRect(1, 1, rootLabel.getWidth()-3, rootLabel.getHeight()-3);
				
			// Draw string (Npe)
			g2d.setColor(Color.BLACK);
			int stringX = (int) ((rootLabel.getWidth() - rect.getWidth())/2.0);
			int stringY = (int) ((rootLabel.getHeight()+rect.getHeight())/2.0);
			g2d.drawString(tn.getNpe(), stringX, stringY);
		}
	}


		
	/* Getting & setting */
	
	public TibNeeg getRoot() {
		return root;
	}

	public void setRoot(TibNeeg root) {
		this.root = root;
	}

	public YawmSaub getYawmSaub() {
		return yawmSaub;
	}

	public void setYawmSaub(YawmSaub yawmSaub) {
		this.yawmSaub = yawmSaub;
	}
	
	public UIOption getOption() {
		return option;
	}

	public void setOption(UIOption option) {
		this.option = option;
	}

	
}
